package com.ganee.agora.view

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.ganee.agora.R
import com.ganee.agora.model.TicketInfoEntity
import com.ganee.agora.viewmodel.TicketDetailViewModel
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_ticket_detail.*
import kotlinx.android.synthetic.main.toolbar_with_title.*
import okhttp3.MediaType
import okhttp3.RequestBody
import org.json.JSONException
import org.json.JSONObject

/**
 * 票据信息页面
 */
@Route(path = Constance.ACTIVITY_URL_TICKET_DETAIL)
class TicketDetailActivity : BaseActivity<TicketDetailViewModel>() ,View.OnClickListener{

    @JvmField
    @Autowired(name = "ticketId")
    var ticketId: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ticket_detail)
        ARouter.getInstance().inject(this)
        iniView()
        initHttpRequest()
    }

    private fun iniView() {
        toolbarCancel.setOnClickListener(this)
        toolbarTitle.text = "Your ticket"
    }

    private fun initHttpRequest() {
        val requestData = JSONObject()
        try {
            requestData.put("ticketId", ticketId)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        val requestBody = RequestBody.create(MediaType.parse("application/json"), requestData.toString())
        viewModel.getTicketInfo(requestBody)
        viewModel.info.observe(this, Observer<TicketInfoEntity> {
            initData(it)
        })
    }

    private fun initData(entity:TicketInfoEntity) {
        Glide.with(this)
                .load(entity.ticketPic)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(ticket_detail_iv_qr_code)

        ticket_detail_tv_title.text = entity.eventTitle ?: ""
        var start:String = ""
        var end:String = ""
        if (!TextUtils.isEmpty(entity.startTime)) {
            start = getTime(entity.startTime!!)
        }
        if (!TextUtils.isEmpty(entity.endTime)) {
            end = getTime(entity.endTime!!)
        }
        ticket_detail_tv_time.text = "$start - $end"
        ticket_detail_tv_location.text = entity.location
        if (entity.amount != null && !TextUtils.isEmpty(entity.amount)&& entity.amount!!.toDouble() > 0){
            ticket_detail_tv_total.text = "$" + entity.amount
        } else {
            ticket_detail_tv_total.text = "Free"
        }
        ticket_detail_tv_number.text = entity.ticketNo
        if (!TextUtils.isEmpty(entity.ticketTime)) {
            ticket_detail_tv_register_time.text = getTime(entity.ticketTime!!)
        } else {
            ticket_detail_tv_register_time.text = "-"
        }
        if (!TextUtils.isEmpty(entity.firstName) && !TextUtils.isEmpty(entity.lastName)){
            ticket_detail_tv_name.text = entity.firstName + " " + entity.lastName
        } else if (!TextUtils.isEmpty(entity.firstName) && TextUtils.isEmpty(entity.lastName)){
            ticket_detail_tv_name.text = entity.firstName
        } else if (TextUtils.isEmpty(entity.firstName) && !TextUtils.isEmpty(entity.lastName)){
            ticket_detail_tv_name.text = entity.lastName
        } else {
            ticket_detail_tv_name.text = "-"
        }
        if (!TextUtils.isEmpty(entity.email)){
            ticket_detail_tv_email.text = entity.email
        } else {
            ticket_detail_tv_email.text = "-"
        }

    }

    override fun getViewModel(): TicketDetailViewModel {
        return ViewModelProviders.of(this@TicketDetailActivity).get(TicketDetailViewModel::class.java)
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.toolbarCancel -> {
                finish()
            }
        }
    }

    private fun getTime(time:String):String{
        val month = time.substring(4, 6).toInt()
        val day = time.substring(6, 8)
        val hour = time.substring(8, 10)
        val minute = time.substring(10, 12)
        var mMonth: String? = null
        when (month) {
            1 -> mMonth = "Jan"
            2 -> mMonth = "Feb"
            3 -> mMonth = "Mar"
            4 -> mMonth = "Apr"
            5 -> mMonth = "May"
            6 -> mMonth = "Jun"
            7 -> mMonth = "Jul"
            8 -> mMonth = "Aug"
            9 -> mMonth = "Sept"
            10 -> mMonth = "Oct"
            11 -> mMonth = "Nov"
            12 -> mMonth = "Dec"
        }
        var mHour: String? = null
        mHour = if (hour.toInt() >= 12) {
            hour + ":" + minute + "pm"
        } else {
            hour + ":" + minute + "am"
        }
        return "$mMonth $day · $mHour"
    }
}
